<?php

namespace app\dc\controller;

use app\common\model\Dc;
use think\Controller;
use think\Db;

class Auth extends Controller
{

    /**
     * 后台登录
     * HYF
     * @return mixed
     */
    public function login()
    {
        if ($this->request->isAjax()) {
            $data = input('');
            // dump($data);
            $vercode = $data['vercode'];
            if (!captcha_check($vercode)) {
                $this->error('验证失败');

            }
            $username = trim($data['username']);
            $password = trim($data['password']);
            if (!$username || !$password) {
                $this->error('数据异常');
            }
            $dcm = new Dc();
            $user = $dcm->where('mobile', $username)->find();
            if (!$user || $user['status'] != 1) {
                $this->error('用户不存在 或状态异常');
            }
            if ($user['password'] != cmf_password($password)) {
                $this->error('验证失败');
            }
            session('dc_id', $user['id']);
            session('dc_info', $user);
            $updata = [];
            $updata['logintime'] = time();
            $updata['loginip'] = request()->ip();

            $dcm->where('id', $user['id'])->update($updata);
            $this->success('验证通过', '', ['access_token' => request()->token()]);
        }

        return $this->fetch();
    }


    // {
    // "code": 0
    // ,"msg": "退出成功"
    // ,"data": null
    // }

    public function logout()
    {

        session('admin_id', null);
        session('admin_info', null);
        return json(['code' => 1, 'msg' => '退出成功']);

    }


}
